package view;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.aop.windows.TransactionalDialog;
import org.uqbar.arena.bindings.NotNullObservable;
import org.uqbar.arena.bindings.ObservableProperty;
import org.uqbar.arena.bindings.PropertyAdapter;
import org.uqbar.arena.layout.ColumnLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.Selector;
import org.uqbar.arena.widgets.TextBox;
import org.uqbar.arena.windows.WindowOwner;
import org.uqbar.lacar.ui.model.ListBuilder;
import org.uqbar.lacar.ui.model.bindings.Binding;

import Transformers.StringToLocalDateTransformer;
import ApplicationModels.AgregarCompromisoAppModel;
import Domain.Empleado;

public class AgregarCompromisoWindow extends
		TransactionalDialog<AgregarCompromisoAppModel> {

	public AgregarCompromisoWindow(WindowOwner owner, AgregarCompromisoAppModel model) {
		super(owner, model);
	}

	@Override
	protected void createFormPanel(Panel mainPanel) {
		this.setTitle("Agregar Compromiso");

		mainPanel.setLayout(new ColumnLayout(1));

		new Label(mainPanel).setText("Empleado: ");

		Selector<Empleado> selector = new Selector<Empleado>(mainPanel) //
				.allowNull(false);
		selector.bindValueToProperty("empleadoSeleccionado");

		Binding<ListBuilder<Empleado>> itemsBinding = selector.bindItems( //
				new ObservableProperty("empleadosDisponibles"));

		itemsBinding.setAdapter( //
				new PropertyAdapter(Empleado.class, "descripcionEntera"));

		new Label(mainPanel).setText("fecha del compromiso");
		new TextBox(mainPanel).bindValueToProperty("fechaCompromiso").setTransformer(new StringToLocalDateTransformer());

		Button aceptar = new Button(mainPanel).setCaption("Aceptar").onClick(
				new MessageSend(this.getModelObject(), "agregarCompromiso"));

		new Button(mainPanel).setCaption("Cerrar").onClick(
				new MessageSend(this, "cerrarDialogo"));


		NotNullObservable elementSelected = new NotNullObservable(
				"empleadoSeleccionado");
		aceptar.bindEnabled(elementSelected);
		

	
	}
	
	public void cerrarDialogo() {
		this.close();
	}
	
	public void agregarCompromiso(){
		this.getModelObject().agregarCompromiso();
		this.accept();
	}

}
